var type = getQueryString("type");// 1 评论  2 转发  3 赞
//评论
function itemsList(){
	  var status ='';
	  if(type == 1){//评论
			status = "agentinfo/findcomment";
		}else if(type == 2){//转发
			status = "agentinfo/findforward";
		}else if(type == 3){//赞
			status = "agentinfo/findpraise";
		}
	  $.get(
	  API_URL+status+"?openid="+openid,
	  function(Obj){
	  	var myData = Obj.basedata.data;
	  	var listStr = '';
	  	for(var i=0;i<myData.length;i++){
	  		//头像
		  	if(myData[i].agent_icon == null || myData[i].agent_icon == ''){
					myData[i].agent_icon ='../img/default.png';
			}
			//图片
			/*
			if(myData[i].photo == null || myData[i].photo == ''){
					myData[i].photo ='../img/default_fang.png';
			}
			*/
			var welldone ='';
			if(myData[i].pa_state == 1){
				welldone = 'welldone';
			}
	  		listStr += '<div class="items_detials">'+
							'<div class="main_content">'+
								'<input type="hidden" value=\"'+myData[i].id+'\" id="itemsId" />'+
								'<input type="hidden" value=\"'+myData[i].agent_openid+'\" id="agent_openId" />'+
								'<div class="head_content">'+
									'<img src="'+myData[i].agent_icon+'" alt="">'+
									'<h3>'+myData[i].agent_name+'<i class=""></i></h3>'+
									'<p>'+myData[i].createtime_str+'</p>'+
									'<input type="hidden" value="'+myData[i].agentid+'">'+
								'</div>';
								if(myData[i].type == 'photo'){
									listStr +='<div class="goto_detials">'+
										'<p class="article clear">'+myData[i].content+'</p>';
										if(myData[i].photo!=null && myData[i].photo!=''){
											//alert(myData[i].photo);
											listStr +='<img class="detials_img" src="'+myData[i].photo+'"/>';
										}
										
									listStr += '</div>';
								}else{
									listStr += '<p class="article clear">'+myData[i].title+'</p>'+
										    '<iframe height="140" width="181" src=\"'+myData[i].url+'\" frameborder=0 allowfullscreen></iframe>'+
											'<ul class="radiolabel_content" >'+
												'<li style="display: inline-block;height: 14px;line-height: 14px;margin: 8px 4px 20px 0;background: #fff;border: none;"><span style="display: inline-block;padding: 3px 5px;text-align: center;border-radius: 3px;font-size: 9px;border: 1px solid #43aaf1;color: #43aaf1;margin: 8px 4px 12px 0;">'+myData[i].channelname+'</span></li>'+
												'<li style="display: inline-block;height: 14px;line-height: 14px;margin: 8px 4px 20px 0;background: #fff;border: none;"><span style="display: inline-block;padding: 3px 5px;text-align: center;border-radius: 3px;font-size: 9px;border: 1px solid #43aaf1;color: #43aaf1;margin: 8px 4px 12px 0;">'+myData[i].curriculumtypename+'</span></li>'+
												'<li style="display: inline-block;height: 14px;line-height: 14px;margin: 8px 4px 20px 0;background: #fff;border: none;"><span style="display: inline-block;padding: 3px 5px;text-align: center;border-radius: 3px;font-size: 9px;border: 1px solid #43aaf1;color: #43aaf1;margin: 8px 4px 12px 0;">'+myData[i].standardname+'</span></li>'+
											'</ul>';
								}
							
							listStr += '<div class="show_window">'+
								'<span><i class="share"></i><em>'+myData[i].forwardcount+'</em></span>'+
								'<span><i class="say"></i><em>'+myData[i].commentcount+'</em></span>'+
								'<span class="wellshow '+welldone+'"><i class="well"></i><em>'+myData[i].praisecount+'</em></span>'+
							'</div></div>';
							if(myData[i].commentcount > 0){
								listStr += '<div class="more_content">'+
											'<p class="more_say" id="more_say">'+
												'<i class="more_icon"></i>查看全部<em>'+myData[i].commentcount+'</em>条评论<span class="into"></span>'+
											'</p>'+
											'<div class="ourtalk_content">';
											var agentItem = myData[i].agent_item;
											//alert(agentItem.length);
											for( var j = 0; j < agentItem.length; j++){
												listStr += '<div class="items clear">'+
																'<h3 class="talk_name">'+agentItem[j].agent_name+'：</h3>'+
																'<p>'+agentItem[j].agent_content+'</p>'+
															'</div>';
											}
								listStr += '</div>';
							}				
				listStr +=  '</div></div>';
 	  	}
 	  	if(type == 1 && listStr == ''){//评论
			$(".page").css('border','none');
 	  		listStr = '<div class="empty_content"><img src="../img/icon_pl.png" alt="" /><p>您还没有评论，快去发表评论吧！</p></div>';
 	  	}else if(type == 2 && listStr == ''){//转发
			$(".page").css('border','none');
 	  		listStr = '<div class="empty_content"><img src="../img/icon_zf.png" alt="" /><p>您还没有转发，喜欢就转发吧！</p></div>';
 	  	}else if(type == 3 && listStr == '') {//赞
			$(".page").css('border','none');
 	  		listStr = '<div class="empty_content"><img src="../img/icon_z.png" alt="" /><p>您还没有点赞，快去点赞吧！</p></div>'
 	  	}
 	  	$(".page").html(listStr);
	  });
}
$(function(){
	itemsList();

	//查看更多评论
	setTimeout(function(){
		$('.head_content').on('click',function(event){
			var agent_openId = $(this).siblings('#agent_openId').val();
			event.stopPropagation(); 
			//alert(agent_openId);
			window.location = "personal_page.html?openid="+agent_openId+'&currentopenid='+openid+'&user_status='+user_status;
		});

		$('.main_content').on('click',function(){
			detialId = $(this).children('#itemsId').val();
			var agent_openId = $(this).children('#agent_openId').val();
			window.location='status_detial.html?agentopenid='+agent_openId+'&openid='+openid+'&detialId='+detialId+'&user_status='+user_status;
		});
		$('.ourtalk_content').on('click',function(){
			$('.main_content').click();
		})
		$('#more_say').on('click',function(){
			$('.main_content').click();
		});

		$('.show_window .share').on('click',function(){
			$('.main_content').click();
		});
		$('.show_window .say').on('click',function(){
			$('.main_content').click();
		});
		
		$('.wellshow').on('click',function(){
			if ($(this).hasClass('welldone')) {
				cancelFun($(this));
			}else {
				wellDoneFun($(this));	
			}
			event.stopPropagation();
		});

	},500);
})


//点赞
function wellDoneFun(wellobj){
	detialId = $(wellobj).parents('.show_window').siblings('#itemsId').val();
	$.ajax({
		url: API_URL+'curriculum/praise?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					$(wellobj).addClass('welldone');
					var numNew = parseInt($(wellobj).text())+ parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}else if (obj.status.code == 1) {
					$(wellobj).addClass('welldone');
					var numNew = parseInt($(wellobj).text())+ parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}
//取消赞
function cancelFun(wellobj){
	detialId = $(wellobj).parents('.show_window').siblings('#itemsId').val();
	$.ajax({
		url: API_URL+'curriculum/cancelpraise?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					$(wellobj).removeClass('welldone');
					var numNew = parseInt($(wellobj).text()) - parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}else if (obj.status.code == 1) {
					$(wellobj).removeClass('welldone');
					var numNew = parseInt($(wellobj).text()) - parseInt(1);
					$(wellobj).html('<i class="well"></i>'+numNew);
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}